package com.study.zzedu.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.SelectiveCourseModel;
import com.study.zzedu.utils.ToastUtil;

import java.util.List;

public class SelectiveCourseAdapter extends RecyclerView.Adapter<SelectiveCourseAdapter.ViewHolder> {
    private List<SelectiveCourseModel> m;



    public class ViewHolder extends RecyclerView.ViewHolder
    {
        private CheckBox rb_check;
        private TextView tv_course_name,tv_course_score,tv_course_detail;
        private ImageView iv_img;
        public ViewHolder(View view){
            super(view);
            rb_check = (CheckBox) view.findViewById(R.id.rb_check);

            iv_img = (ImageView) view.findViewById(R.id.iv_img);

            tv_course_name = (TextView) view.findViewById(R.id.tv_course_name);
            tv_course_score = (TextView) view.findViewById(R.id.tv_course_score);
            tv_course_detail = (TextView) view.findViewById(R.id.tv_course_detail);
        }
    }

    public SelectiveCourseAdapter(List<SelectiveCourseModel> yModel) {
        m = yModel;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int Viewtype) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.selective_course_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(m.size()<1)
            return;
        SelectiveCourseModel yModel = m.get(position);
        holder.tv_course_name.setText(yModel.getName());
        holder.tv_course_score.setText(String.valueOf(yModel.getScore()));
        holder.tv_course_detail.setText(yModel.getIntroduce());
        if("已选".equals(yModel.getStatus())){
            holder.rb_check.setChecked(true);
        }else {
            holder.rb_check.setChecked(false);
        }
        holder.rb_check.setOnCheckedChangeListener((new OnMyCheckBtn(position)));
    }

    public int getItemCount() {
        return m.size();
    }

    class OnMyCheckBtn implements CompoundButton.OnCheckedChangeListener{

        private int position;
        public OnMyCheckBtn(int position){
            this.position = position;
        }
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(onC != null)
                onC.onChan(this.position,b);
            if(b)
                m.get(position).setStatus("已选");
            else
                m.get(position).setStatus("未选");
        }
    }

    public OnChange onC;
    public void setOnCheckChange(OnChange on){
        this.onC = on;
    }
    public interface OnChange{
        void onChan(int posi,boolean b);
    }
}